﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{
    [Description("其他应付单保存前校验OA数据库更新金额")]
    [HotUpdate]
    public class OtherPaySaveEdit : AbstractBillPlugIn
    {

        public override void BeforeSave(BeforeSaveEventArgs e)
        {
            base.BeforeSave(e);
            var userId = this.Context.UserId;//当前用户ID

            if (userId.ToString() == "16394")
            {
                var value = (DynamicObject)this.Model.GetValue("FBILLTYPEID");
                string FBILLTYPEID = value["Number"].ToString();//单据类型编码
                if (FBILLTYPEID.EqualsIgnoreCase("QTYFD03_SYS") || FBILLTYPEID.EqualsIgnoreCase("QTYFD04_SYS"))
                {

                    if (!this.Model.GetValue("F_DEV_OABILL").ToString().IsNullOrEmpty() && this.Model.GetValue("F_DEV_OABILL").ToString().EqualsIgnoreCase("话费报销单"))
                    {
                        EntryEntity entryEntity = this.View.BusinessInfo.GetEntryEntity("FEntity");//表体名称
                        DynamicObjectCollection entryRows = this.View.Model.GetEntityDataObject(entryEntity);
                        foreach (DynamicObject entryRow in entryRows)//循环单据体遍历
                        {
                            int FSEQ = Int32.Parse(entryRow["Seq"].ToString());//行号
                            string F_DEV_OAEntryId = entryRow["F_DEV_OAEntryId"].ToString();//OA明细唯一码
                            string sql = string.Format(@"/*dialect*/ SELECT*FROM [CRM].[ecology].[dbo].[formtable_main_402_dt1] where id='" + F_DEV_OAEntryId + "'");
                            DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                            if (sql_toStirng.Rows.Count > 0)
                            {
                                double bhsje = double.Parse(sql_toStirng.Rows[0]["bhsje"].ToString());//不含税金额
                                double se = double.Parse(sql_toStirng.Rows[0]["se"].ToString());//税额
                                double jshj = double.Parse(sql_toStirng.Rows[0]["jshj"].ToString());//价税合计
                                double sbje = double.Parse(sql_toStirng.Rows[0]["sbje"].ToString());//实报金额
                                if (sbje != jshj)
                                {

                                    this.View.Model.SetValue("FTOTALAMOUNTFOR", sbje, FSEQ - 1);
                                    this.View.InvokeFieldUpdateService("FTOTALAMOUNTFOR", FSEQ - 1);
                                    this.View.Model.SetValue("FNOTAXAMOUNTFOR", sbje - se, FSEQ - 1);
                                    this.View.InvokeFieldUpdateService("FNOTAXAMOUNTFOR", FSEQ - 1);

                                }
                                this.View.Model.SetValue("F_DEV_NOTAXAMOUNTFOR", bhsje, FSEQ - 1);
                                this.View.Model.SetValue("F_DEV_TOTALAMOUNTFOR", jshj, FSEQ - 1);
                            }
                        }
                        updateAmount();
                    }

                    if (!this.Model.GetValue("F_DEV_OABILL").ToString().IsNullOrEmpty() && this.Model.GetValue("F_DEV_OABILL").ToString().EqualsIgnoreCase("个人报销"))
                    {
                        EntryEntity entryEntity = this.View.BusinessInfo.GetEntryEntity("FEntity");//表体名称
                        DynamicObjectCollection entryRows = this.View.Model.GetEntityDataObject(entryEntity);
                        foreach (DynamicObject entryRow in entryRows)//循环单据体遍历
                        {
                            int FSEQ = Int32.Parse(entryRow["Seq"].ToString());//行号
                            string F_DEV_OAEntryId = entryRow["F_DEV_OAEntryId"].ToString();//OA明细唯一码
                            string sql = string.Format(@"/*dialect*/SELECT*FROM [CRM].[ecology].[dbo].[formtable_main_315_dt4] where id='" + F_DEV_OAEntryId + "'");
                            DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                            if (sql_toStirng.Rows.Count > 0)
                            {
                                double bhsje = double.Parse(sql_toStirng.Rows[0]["bhsje"].ToString());//不含税金额
                                double se = double.Parse(sql_toStirng.Rows[0]["se"].ToString());//税额
                                double jshj = double.Parse(sql_toStirng.Rows[0]["jshj"].ToString());//价税合计
                                double sbje = double.Parse(sql_toStirng.Rows[0]["sbje"].ToString());//实报金额
                                if (sbje != jshj)
                                {

                                    this.View.Model.SetValue("FTOTALAMOUNTFOR", sbje, FSEQ - 1);
                                    this.View.InvokeFieldUpdateService("FTOTALAMOUNTFOR", FSEQ - 1);
                                    this.View.Model.SetValue("FNOTAXAMOUNTFOR", sbje - se, FSEQ - 1);
                                    this.View.InvokeFieldUpdateService("FNOTAXAMOUNTFOR", FSEQ - 1);

                                }
                                this.View.Model.SetValue("F_DEV_NOTAXAMOUNTFOR", bhsje, FSEQ - 1);
                                this.View.Model.SetValue("F_DEV_TOTALAMOUNTFOR", jshj, FSEQ - 1);
                            }
                        }
                        updateAmount();
                    }
                    if (!this.Model.GetValue("F_DEV_OABILL").ToString().IsNullOrEmpty() && this.Model.GetValue("F_DEV_OABILL").ToString().EqualsIgnoreCase("差旅报销"))
                    {
                        EntryEntity entryEntity = this.View.BusinessInfo.GetEntryEntity("FEntity");//表体名称
                        DynamicObjectCollection entryRows = this.View.Model.GetEntityDataObject(entryEntity);
                        foreach (DynamicObject entryRow in entryRows)//循环单据体遍历
                        {
                            int FSEQ = Int32.Parse(entryRow["Seq"].ToString());//行号
                            string F_DEV_OAEntryId = entryRow["F_DEV_OAEntryId"].ToString();//OA明细唯一码
                            string sql = string.Format(@"/*dialect*/SELECT*FROM [CRM].[ecology].[dbo].[formtable_main_274_dt1] where id='" + F_DEV_OAEntryId + "'");
                            DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                            if (sql_toStirng.Rows.Count > 0)
                            {
                                double bhsje = double.Parse(sql_toStirng.Rows[0]["bhsje"].ToString());//不含税金额
                                double se = double.Parse(sql_toStirng.Rows[0]["se"].ToString());//税额
                                double jshj = double.Parse(sql_toStirng.Rows[0]["jshj"].ToString());//价税合计
                                double sbje = double.Parse(sql_toStirng.Rows[0]["sbje"].ToString());//实报金额

                                if (sbje != jshj)
                                {
                                    this.View.Model.SetValue("FTOTALAMOUNTFOR", sbje, FSEQ - 1);
                                    this.View.InvokeFieldUpdateService("FTOTALAMOUNTFOR", FSEQ - 1);
                                    this.View.Model.SetValue("FNOTAXAMOUNTFOR", sbje - se, FSEQ - 1);
                                    this.View.InvokeFieldUpdateService("FNOTAXAMOUNTFOR", FSEQ - 1);
                                }

                                this.View.Model.SetValue("F_DEV_NOTAXAMOUNTFOR", bhsje, FSEQ - 1);
                                this.View.InvokeFieldUpdateService("F_DEV_NOTAXAMOUNTFOR", FSEQ - 1);
                                this.View.Model.SetValue("F_DEV_TOTALAMOUNTFOR", jshj, FSEQ - 1);
                                this.View.InvokeFieldUpdateService("F_DEV_TOTALAMOUNTFOR", FSEQ - 1);
                            }
                            if (entryRow["INVOICETYPE"].ToString().Equals("E") && entryRow["F_QLTK_INVOICENAME"] == null)
                            {
                                string sql1 = string.Format(@"/*dialect*/ SELECT * FROM [CRM].[ecology].[dbo].[formtable_main_274_dt4] where id='" + F_DEV_OAEntryId + "'");
                                DataTable sql_toStirng1 = DBUtils.ExecuteDataSet(this.Context, sql1).Tables[0];
                                if (sql_toStirng1.Rows.Count > 0)
                                {
                                    double bxje = double.Parse(sql_toStirng1.Rows[0]["bxje"].ToString());//报销金额
                                    this.View.Model.SetValue("F_DEV_NOTAXAMOUNTFOR", bxje, FSEQ - 1);
                                    this.View.Model.SetValue("F_DEV_TOTALAMOUNTFOR", bxje, FSEQ - 1);
                                }
                            }
                        }
                        updateAmount();
                    }
                }
            }
        }
        public void updateAmount()
        {
            var TOTALAMOUNTFOR_P = Convert.ToDecimal(this.View.Model.GetValue("TOTALAMOUNTFOR_P"));
            var F_QLTK_PaymentCount = Convert.ToDecimal(this.View.Model.GetValue("F_QLTK_PaymentCount"));
            if (TOTALAMOUNTFOR_P != F_QLTK_PaymentCount)
            {
                var bhs = 0m;
                var zje = 0m;
                EntryEntity entry = this.View.BusinessInfo.GetEntryEntity("FEntity");//表体名称
                DynamicObjectCollection entrys = this.View.Model.GetEntityDataObject(entry);
                foreach (DynamicObject entryRow in entrys)//循环单据体遍历
                {
                    // 遍历单据体数据包对某一列求和

                    bhs += Convert.ToDecimal(entryRow["NOTAXAMOUNTFOR"]);//不含税金额
                    zje += Convert.ToDecimal(entryRow["TOTALAMOUNTFOR"]);//总金额
                }
                this.View.Model.SetValue("FNOTAXAMOUNT", bhs);//不含税金额本位币
                this.View.Model.SetValue("FTOTALAMOUNT_H", zje);//含税金额本位币
                this.View.Model.SetValue("FNOTSETTLEAMOUNT", zje);//未借款金额本位币
                this.View.Model.SetValue("FTOTALAMOUNTFOR_H", zje);//总金额
                this.View.Model.SetValue("FNOTSETTLEAMOUNTFOR", zje);//未借款金额
            }
        }
    }
}
